//
//  ReadPageViewController.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/12.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "ReadPageViewController.h"
#import "CustomerNetworking.h"
#import "ReadPageModel.h" 
#import "ReadPageCollectionViewCell.h"
#import <SDCycleScrollView.h>
#import "LoopsModel.h"
#import "LoopsDetailViewController.h"
#import "ReadSecDetailViewController.h"
#import <MBProgressHUD.h>

#define w self.view.frame.size.width
#define h self.view.frame.size.height

@interface ReadPageViewController ()<UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, SDCycleScrollViewDelegate, MBProgressHUDDelegate>
//阅读集数组
@property (nonatomic, retain) NSMutableArray * totalArray;
//轮播图图片地址数组
@property (nonatomic, retain) NSMutableArray * loopsArray;
//轮播图Model数组
@property (nonatomic, retain) NSMutableArray * loopsDetailArray;
//阅读View
@property (nonatomic, retain) UICollectionView * readCollectionView;
//轮播图
@property (nonatomic, retain) SDCycleScrollView * loops;
@property (nonatomic, retain) MBProgressHUD *HUD;


@end

@implementation ReadPageViewController
-(void)dealloc
{
    [_totalArray release];
    [_loops release];
    [_loopsArray release];
    [_readCollectionView release];
    [_loopsDetailArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"心灵驿站";
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Read_back.jpg"]];
//    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
    self.totalArray = [NSMutableArray array];
    self.loopsArray = [NSMutableArray array];
    self.loopsDetailArray = [NSMutableArray array];
    //轮播图解析以及创建
    [self loadLoopsView];
    //阅读集
    [self loadCollectionData];
    [self createACollectionView];
    
    
}
- (void)loadLoopsView
{
    self.HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    self.HUD.delegate = self;
    //显示的文字
    self.HUD.labelText = @"正在进行加载~";
    
    
    NSDictionary *body = @{@"client":@"1",@"deviceid":@"3E0E6892-8007-4A19-956A-658542F9DF2C", @"version":@"3.0.6"};
    [CustomerNetworking customerWithURL:@"http://api2.pianke.me/read/columns" andParamer:body andBlock:^(id response) {        for (NSDictionary * dic  in [[response objectForKey:@"data"]objectForKey:@"carousel"]) {
            [self.loopsArray addObject:[dic objectForKey:@"img"]];
            LoopsModel * model = [LoopsModel modelWithDictionary:dic];
            [self.loopsDetailArray addObject:model];
            //网络加载 --- 创建带标题的图片轮播器
            self.loops = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, w, 220) imageURLStringsGroup:nil]; // 模拟网络延时情景
            self.loops.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
            self.loops.delegate = self;
            //    self.loops.titlesGroup = self.titleArr;
            self.loops.dotColor = [UIColor colorWithRed:203 / 255.0 green:203 / 255.0  blue:253 / 255.0  alpha:1]; // 自定义分页控件小圆标颜色
            self.loops.placeholderImage = [UIImage imageNamed:@"Read_zhanwei"];
            self.loops.autoScrollTimeInterval = 3.0;
            self.loops.imageURLStringsGroup = self.loopsArray;
            [self.view addSubview:self.loops];
        }

    }];
    [self.HUD hide:YES];

}
//轮播图的点击事件调用
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    NSLog(@"我是轮播图点击事件---点击了第%ld张图片", index + 1);
    LoopsDetailViewController * detail = [[LoopsDetailViewController alloc]init];
    LoopsModel * model = [self.loopsDetailArray objectAtIndex:index];
    detail.loopId = model.ID;
    detail.url = model.url;
    detail.img = model.img;
    [self.navigationController pushViewController:detail animated:YES];
}
- (void)loadCollectionData
{
    NSDictionary *body = @{@"client":@"1",@"deviceid":@"3E0E6892-8007-4A19-956A-658542F9DF2C", @"version":@"3.0.6"};
    [CustomerNetworking customerWithURL:@"http://api2.pianke.me/read/columns" andParamer:body andBlock:^(id response) {
        for (NSDictionary * dic  in [[response objectForKey:@"data"]objectForKey:@"list"]) {
            ReadPageModel * model = [ReadPageModel modelWithDictionary:dic];
            [self.totalArray addObject:model];
    }
        [self.readCollectionView reloadData];
    }];
}

- (void)createACollectionView
{
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    self.readCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 188, w, h - 188 - 32) collectionViewLayout:flowLayout];
//    self.readCollectionView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    self.readCollectionView.backgroundColor = [UIColor clearColor];
//    self.readCollectionView.pagingEnabled = YES;
    self.readCollectionView.delegate = self;
    self.readCollectionView.dataSource = self;
    self.readCollectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.readCollectionView];
    [_readCollectionView release];
    [self.readCollectionView registerClass:[ReadPageCollectionViewCell class] forCellWithReuseIdentifier:@"readIden"];
}
//返回最小的左右间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
//返回最小上下间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(w / 4 * 3, h - 188 - 34);
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.totalArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ReadPageCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"readIden" forIndexPath:indexPath];
    ReadPageModel * model = [self.totalArray objectAtIndex:indexPath.item];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ReadSecDetailViewController * detail = [[ReadSecDetailViewController alloc]init];
    ReadPageModel * model = [self.totalArray objectAtIndex:indexPath.item];
    detail.readId = [NSString stringWithFormat:@"%@", model.type];
    NSLog(@"====%@", detail.readId);
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
